<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "mkt_feedback".
 *
 * @property string $id
 * @property string $user_id
 * @property string $content
 * @property string $create_time
 */
class MktFeedback extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mkt_feedback';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id'], 'integer'],
            [['content'], 'required'],
            [['content'], 'string'],
            [['create_time'], 'safe']
        ];
    }

    public function behaviors() {
        return [
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'createdAtAttribute' => 'create_time',
                'updatedAtAttribute' => 'create_time',
                'value' => new \yii\db\Expression('NOW()'),
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'content' => '建议',
            'create_time' => '时间',
        ];
    }

    /**
     * @inheritdoc
     * @return MktFeedbackQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MktFeedbackQuery(get_called_class());
    }

    public static function sendMessageAjax($params)
    {
        $model = new \common\models\MktFeedback;
        $model->loadDefaultValues();
        $model->user_id = $params['user_id'];
        $model->content = $params['content'];
        if ($model->save()) {
            return \yii\helpers\Json::encode(['code' => 0, 'msg' => 'OK']);
        }
        yii::error(print_r($model->getErrors(), true));
        return \yii\helpers\Json::encode(['code' => 1, 'msg' => '保存错误']);        
    }
    
}
